package com.lry.crawlers.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.comic.entity.ComicChapter;
import com.lry.crawlers.comic.mapper.ComicChapterMapper;
import com.lry.crawlers.comic.service.ComicChapterService;

import com.lry.crawlers.comic.entity.ComicChapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComicChapterServiceImpl implements ComicChapterService {

    @Autowired
    ComicChapterMapper comicChapterMapper;


    @Override
    public int setComicChapter(ComicChapter comicChapter) {
        int insert = comicChapterMapper.insert(comicChapter);
        return insert;
    }

    @Override
    public int setComicChapters(List<ComicChapter> comicChapters) {
        int i = comicChapterMapper.insertBatchSomeColumn(comicChapters);
        return i;
    }

    @Override
    public List<ComicChapter> selectByComicId(String comicId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("comic_id",comicId);
        List<ComicChapter> list = comicChapterMapper.selectList(wrapper);
        if(list!=null){
            return list;
        }
        return null;
    }

    @Override
    public List<Integer> removeDuplicates(List<String> titles, List<ComicChapter> comicChapters) {
        List<Integer> duplicates = new ArrayList<>();
        List<String> titlesy = new ArrayList<>();
        for (ComicChapter comicChapter : comicChapters) {
            titlesy.add(comicChapter.getTitle().trim());
        }
        for(int i = 0;i<titles.size();i++){
            boolean contains = titlesy.contains(titles.get(i));
            if(contains){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }

    @Override
    public int setComicChapterUp(ComicChapter comicChapter) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id",comicChapter.getChapterId());
        return comicChapterMapper.update(comicChapter,wrapper);
    }

}



